﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace Runsheet_Automater
{
    public static class Email
    {
        // Fields
        private static string emailBody;

        // Methods
        static Email()
        {
            if (null == emailBody)
            {
                emailBody = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "EmailTemplate.htm").ReadToEnd();
            }
        }


        public static void Send(string status)
        {
            try
            {
                MailMessage mail = new MailMessage();
                MailAddress address = new MailAddress("RunsheetAutomater@Alahli.com", "Runsheet Automater");
                mail.From = address;
                Attachment item = new Attachment(AppDomain.CurrentDomain.BaseDirectory + "Logo_NCB.GIF")
                {
                    ContentDisposition = { Inline = true, DispositionType = "inline" }
                };
                mail.To.Add("754783@alahli.com");
                mail.To.Add("151227@alahli.com");
                mail.Subject = "Runsheet Automater"; ;
                mail.IsBodyHtml = true;
                mail.Body = emailBody;                
                string str = Path.GetFileName(AppDomain.CurrentDomain.BaseDirectory + "Logo_NCB.GIF").Replace(".", "") + "@alahli";
                string path = Environment.CurrentDirectory + @"Logo_NCB.GIF";
                item.ContentId = str;
                item.ContentType.MediaType = "image/gif";
                item.ContentType.Name = Path.GetFileName(path);
                mail.Attachments.Add(item);
                mail.Body = mail.Body.Replace("@@IMAGE@@", "cid:" + str);
                mail.Body = mail.Body.Replace("@@STATUS@@", status);
                SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], 0x19)
                {
                    UseDefaultCredentials = false
                };
                NetworkCredential credential = new NetworkCredential("ncbapp", "P@ssw0rd");
                client.Credentials = credential;
                client.Send(mail);
                mail.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}, EMAIL ERROR:: Desc: {1}", DateTime.Now.ToString(), ex.ToString());
            }
        }
    }
}